package com.ruoyi.myResitExam.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.resitExam.domain.SysResitExam;
import com.ruoyi.myResitExam.service.IMyResitExamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 我的补考Controller
 * 
 * @author ruoyi
 * @date 2023-05-06
 */
@RestController
@RequestMapping("/myResitExam")
public class MyResitExamController extends BaseController {
  @Autowired
  private IMyResitExamService myResitExamService;

  /**
   * 查询我的补考列表
   */
  @GetMapping("/list")
  public TableDataInfo list(SysResitExam sysResitExam) {
    startPage();
    // 设置当前用户ID作为查询条件
    sysResitExam.setUserId(SecurityUtils.getUserId());
    List<SysResitExam> list = myResitExamService.selectMyResitExamList(sysResitExam);
    return getDataTable(list);
  }

  /**
   * 导出我的补考列表
   */
  @Log(title = "我的补考", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, SysResitExam sysResitExam) {
    // 设置当前用户ID作为查询条件
    sysResitExam.setUserId(SecurityUtils.getUserId());
    List<SysResitExam> list = myResitExamService.selectMyResitExamList(sysResitExam);
    ExcelUtil<SysResitExam> util = new ExcelUtil<SysResitExam>(SysResitExam.class);
    util.exportExcel(response, list, "我的补考数据");
  }

  /**
   * 获取我的补考详细信息
   */
  @GetMapping(value = "/{resitId}")
  public AjaxResult getInfo(@PathVariable("resitId") Integer resitId) {
    return success(myResitExamService.selectMyResitExamByResitId(resitId, SecurityUtils.getUserId()));
  }

  /**
   * 申请补考
   */
  @Log(title = "我的补考", businessType = BusinessType.INSERT)
  @PostMapping("/apply")
  public AjaxResult add(@RequestBody SysResitExam sysResitExam) {
    // 设置当前用户ID
    sysResitExam.setUserId(SecurityUtils.getUserId());
    // 设置初始状态为待审核
    sysResitExam.setResitStatus("0");
    return toAjax(myResitExamService.insertMyResitExam(sysResitExam));
  }

  /**
   * 取消补考申请
   */
  @Log(title = "我的补考", businessType = BusinessType.UPDATE)
  @PutMapping("/cancel/{resitId}")
  public AjaxResult cancel(@PathVariable("resitId") Integer resitId) {
    return toAjax(myResitExamService.cancelMyResitExam(resitId, SecurityUtils.getUserId()));
  }
}